<?php
class Admin_Model_Server extends Zend_Db_Table_Abstract
{
	/**
	 * Model of Server modules
	 * @author Nguyen Manh Hung (manhhung86it@gmail.com)
	 */
	
	protected $_name = TABLE_NHK_SERVER;
	
	/**
	 * @author Nguyen Manh Hung (manhhung86it@gmail.com)
	 * @param $name
	 * @param $url
	 * @param $status
	 */
	public function insertServer($name, $url='', $status =1)
	{
		$dateCreated = new Zend_Db_Expr('NOW()');
		$db = Zend_Db_Table::getDefaultAdapter();
		$row = $this->createRow();
		$row->name = $name;
		if ($url) {
			$row->url_server = $url;
		}
		$row->status = $status;
		$row->date_created = $dateCreated;
		$row->save();
		$id =$row->id;
		return $id;
	}
	
	/**
	 * @author Nguyen Manh Hung (manhhung86it@gmail.com)
	 * @param $id
	 * @param $name
	 * @param $url
	 * @param $status
	 */
	public function updateServer($id, $name, $url='', $status =1)
	{
		$db = Zend_Db_Table::getDefaultAdapter();
		$row = $this->find($id)->current();
		if($row){
			$row->name = $name;
			if ($url) {
				$row->url_server = $url;
			}else{
				$row->url_server = '';
			}
			$row->status = $status;
			$row->save();
			return 1;
		}else{
			return 0;
		}
	}
	
	
	/**
	 * delete server 
	 * @author Nguyen Manh Hung (manhhung86it@gmail.com)
	 * @param Int $id
	 */
	public function deleteServer($id){
		$db = Zend_Db_Table::getDefaultAdapter();
		$id = $db->quote($id, INTEGER);
		$rowMenu = $this->find($id)->current();
		if($rowMenu){
			$rowMenu->delete();
			return 1;
		}else{
			return 0;
		}
	}
	
	/**
	 * fetch all server
	 * @author Nguyen Manh Hung (manhhung86it@gmail.com)
	 * @param Array $filters
	 * @param String $sortFeild
	 * @param Int $limit
	 * @param Int $page
	 */
	public function fetchServer($filters = array(), $sortFeild = null, $limit = null, $page = 1)
	{
		$select = $this->select();
		//add cac filter vao truy van tim kiem
		if(count($filters)>0)
		{
			foreach ($filters as $feild => $filter){
				$select->where($feild .' =?', $filter);
			}
		}
		//add sort vao truy van
		if(null != $sortFeild)
		{
			$select->order($sortFeild);
		}
		//add limit - offset vao truy van		
		if(null != $limit)
		{
			$offset = $page * NUM_PER_PAGE;
			if($offset<=$limit)
			{
				$offset = $limit+NUM_PER_PAGE;
			}
			$select->limit($limit, $offset);
		}		
		return $this->fetchAll($select);
	}
	 
}